<template>
	<view class="address-container">
		<!-- 导航栏 -->
		<view class="nav-bar">
			<view class="nav-left" @click="goBack">
				<text class="iconfont icon-arrow-left"></text>
			</view>
			<view class="nav-title">收货地址</view>
			<view class="nav-right" @click="addAddress">
				<text class="iconfont icon-plus"></text>
			</view>
		</view>
		
		<!-- 地址列表 -->
		<scroll-view 
			scroll-y 
			class="address-list"
			refresher-enabled
			@refresherrefresh="onRefresh"
			:refresher-triggered="refreshing"
		>
			<view 
				v-for="item in addressList" 
				:key="item.id"
				class="address-item"
				@click="selectAddress(item)"
			>
				<!-- 默认标签 -->
				<view v-if="item.isDefault" class="default-tag">
					默认
				</view>
				
				<!-- 地址信息 -->
				<view class="address-info">
					<view class="contact-info">
						<text class="contact-name">{{ item.name }}</text>
						<text class="contact-phone">{{ item.phone }}</text>
					</view>
					<view class="address-detail">
						{{ item.province }} {{ item.city }} {{ item.district }} {{ item.detail }}
					</view>
				</view>
				
				<!-- 操作按钮 -->
				<view class="address-actions">
					<view class="action-btn" @click.stop="editAddress(item)">
						<text class="iconfont icon-edit"></text>
					</view>
					<view class="action-btn" @click.stop="deleteAddress(item)">
						<text class="iconfont icon-delete"></text>
					</view>
				</view>
			</view>
			
			<!-- 空状态 -->
			<view v-if="isEmpty" class="empty-state">
				<image src="/static/images/empty-address.png" class="empty-image" />
				<view class="empty-text">暂无收货地址</view>
				<view class="empty-desc">添加收货地址，享受便捷购物体验</view>
				<view class="empty-btn" @click="addAddress">
					添加地址
				</view>
			</view>
		</scroll-view>
		
		<!-- 底部添加按钮 -->
		<view v-if="!isEmpty" class="add-address-btn" @click="addAddress">
			<text class="iconfont icon-plus"></text>
			<text class="btn-text">新增收货地址</text>
		</view>
	</view>
</template>

<script>
import api from '@/api'

export default {
	data() {
		return {
			addressList: [],
			loading: false,
			refreshing: false,
			// 是否为选择地址模式（从订单页面跳转过来）
			selectMode: false
		}
	},
	
	computed: {
		// 是否为空
		isEmpty() {
			return this.addressList.length === 0 && !this.loading
		}
	},
	
	onLoad(options) {
		// 检查是否为选择地址模式
		if (options.select === 'true') {
			this.selectMode = true
		}
		
		this.loadAddressList()
	},
	
	onShow() {
		// 从添加/编辑地址页面返回时刷新列表
		this.loadAddressList()
	},
	
	onPullDownRefresh() {
		this.onRefresh()
	},
	
	methods: {
		// 返回
		goBack() {
			uni.navigateBack()
		},
		
		// 加载地址列表
		async loadAddressList() {
			if (this.loading) return
			
			this.loading = true
			
			try {
				// 模拟API调用
				const result = await this.mockGetAddressList()
				this.addressList = result.data
			} catch (error) {
				uni.showToast({
					title: error.message || '加载失败',
					icon: 'none'
				})
			} finally {
				this.loading = false
				this.refreshing = false
				uni.stopPullDownRefresh()
			}
		},
		
		// 模拟获取地址列表
		mockGetAddressList() {
			return new Promise((resolve) => {
				setTimeout(() => {
					resolve({
						data: [
							{
								id: 1,
								name: '张三',
								phone: '13800138000',
								province: '广东省',
								city: '深圳市',
								district: '南山区',
								detail: '科技园南区深南大道10000号腾讯大厦',
								isDefault: true
							},
							{
								id: 2,
								name: '李四',
								phone: '13900139000',
								province: '北京市',
								city: '北京市',
								district: '海淀区',
								detail: '中关村软件园二期东北旺西路8号中关村软件园',
								isDefault: false
							},
							{
								id: 3,
								name: '王五',
								phone: '13700137000',
								province: '上海市',
								city: '上海市',
								district: '浦东新区',
								detail: '陆家嘴环路1000号恒生银行大厦',
								isDefault: false
							}
						]
					})
				}, 500)
			})
		},
		
		// 刷新
		onRefresh() {
			this.refreshing = true
			this.loadAddressList()
		},
		
		// 选择地址
		selectAddress(item) {
			if (this.selectMode) {
				// 选择地址模式，返回选中的地址
				uni.$emit('addressSelected', item)
				uni.navigateBack()
			}
		},
		
		// 添加地址
		addAddress() {
			uni.navigateTo({
				url: '/pages/profile/address-edit'
			})
		},
		
		// 编辑地址
		editAddress(item) {
			uni.navigateTo({
				url: `/pages/profile/address-edit?id=${item.id}`
			})
		},
		
		// 删除地址
		deleteAddress(item) {
			uni.showModal({
				title: '确认删除',
				content: '确定要删除这个收货地址吗？',
				success: (res) => {
					if (res.confirm) {
						this.confirmDeleteAddress(item.id)
					}
				}
			})
		},
		
		// 确认删除地址
		async confirmDeleteAddress(id) {
			try {
				// 模拟删除API
				await new Promise(resolve => setTimeout(resolve, 500))
				
				// 从列表中移除
				const index = this.addressList.findIndex(item => item.id === id)
				if (index > -1) {
					const deletedItem = this.addressList[index]
					this.addressList.splice(index, 1)
					
					// 如果删除的是默认地址，设置第一个地址为默认
					if (deletedItem.isDefault && this.addressList.length > 0) {
						this.addressList[0].isDefault = true
					}
				}
				
				uni.showToast({
					title: '删除成功',
					icon: 'success'
				})
			} catch (error) {
				uni.showToast({
					title: '删除失败',
					icon: 'none'
				})
			}
		}
	}
}
</script>

<style lang="scss" scoped>
.address-container {
	min-height: 100vh;
	background: #f5f5f5;
	padding-top: var(--status-bar-height);
	padding-bottom: env(safe-area-inset-bottom);
}

.nav-bar {
	display: flex;
	align-items: center;
	justify-content: space-between;
	height: 44px;
	padding: 0 15px;
	background: #fff;
	border-bottom: 1px solid #eee;
	
	.nav-left,
	.nav-right {
		width: 44px;
		height: 44px;
		display: flex;
		align-items: center;
		justify-content: center;
		
		.iconfont {
			font-size: 20px;
			color: #333;
		}
	}
	
	.nav-title {
		font-size: 18px;
		font-weight: 500;
		color: #333;
	}
}

.address-list {
	height: calc(100vh - var(--status-bar-height) - 44px - 80px);
	padding: 10px;
}

.address-item {
	background: #fff;
	border-radius: 8px;
	padding: 20px;
	margin-bottom: 10px;
	position: relative;
	display: flex;
	align-items: flex-start;
	justify-content: space-between;
	
	.default-tag {
		position: absolute;
		top: 0;
		left: 0;
		background: linear-gradient(135deg, #ff6b6b, #ff8e8e);
		color: #fff;
		font-size: 12px;
		padding: 4px 12px;
		border-radius: 8px 0 8px 0;
	}
	
	.address-info {
		flex: 1;
		padding-top: 10px;
		
		.contact-info {
			display: flex;
			align-items: center;
			margin-bottom: 8px;
			
			.contact-name {
				font-size: 16px;
				color: #333;
				font-weight: 500;
				margin-right: 12px;
			}
			
			.contact-phone {
				font-size: 14px;
				color: #666;
			}
		}
		
		.address-detail {
			font-size: 14px;
			color: #666;
			line-height: 1.5;
		}
	}
	
	.address-actions {
		display: flex;
		flex-direction: column;
		gap: 8px;
		margin-left: 15px;
		
		.action-btn {
			width: 32px;
			height: 32px;
			border-radius: 16px;
			background: #f0f0f0;
			display: flex;
			align-items: center;
			justify-content: center;
			
			.iconfont {
				font-size: 16px;
				color: #666;
			}
			
			&:last-child .iconfont {
				color: #ff4757;
			}
		}
	}
}

.empty-state {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	padding: 60px 20px;
	
	.empty-image {
		width: 120px;
		height: 120px;
		margin-bottom: 20px;
	}
	
	.empty-text {
		font-size: 16px;
		color: #666;
		margin-bottom: 8px;
	}
	
	.empty-desc {
		font-size: 14px;
		color: #999;
		margin-bottom: 30px;
	}
	
	.empty-btn {
		padding: 12px 24px;
		background: #007aff;
		color: #fff;
		border-radius: 20px;
		font-size: 14px;
	}
}

.add-address-btn {
	position: fixed;
	bottom: 20px;
	left: 20px;
	right: 20px;
	height: 50px;
	background: #007aff;
	border-radius: 25px;
	display: flex;
	align-items: center;
	justify-content: center;
	gap: 8px;
	margin-bottom: env(safe-area-inset-bottom);
	
	.iconfont {
		font-size: 18px;
		color: #fff;
	}
	
	.btn-text {
		font-size: 16px;
		color: #fff;
		font-weight: 500;
	}
}
</style>